package org.apache.struts2.plugin.json.model;

import java.util.HashMap;
import java.util.Map;

/**
 * Definition of the JSON Object type.
 * @author VERDOÏA Laurent <verdoialaurent@gmail.com>
 */
public class JSONObject extends JSONValue<Map<JSONString, JSONValue>> {

    /**
     * Wrapper constructor for {@link JSONValue#JSONValue}
     * @see JSONValue#JSONValue
     */
    public JSONObject(Map<JSONString, JSONValue> data) {
        super(data);
    }

    /**
     * Wrapper constructor for {@link JSONValue#JSONValue(boolean)}
     * @see JSONValue#JSONValue
     */
    public JSONObject(boolean nullity) {
        super(nullity);
    }

    /**
     * Wrapper constructor for {@link JSONValue#JSONValue()}
     * @see JSONValue#JSONValue
     */
    public JSONObject() {
        super();
    }

    /**
     * Add an entry inside the object. <br />
     * The both key and and value cannot be null value. The key cannot be a 
     * null valued JSON String.
     * @param key Key used for the object
     * @param value Value assigned to the key
     */
    public void add(JSONString key, JSONValue value) {
        if (key == null || key.isNull() == true || value == null) {
            throw new NullPointerException("Parameter must not be null");
        }
        setNullity(false);
        this.data.put(key, value);
    }

    @Override
    protected Map<JSONString, JSONValue> instanciateData() {
        return new HashMap<JSONString, JSONValue>();
    }
}
